<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Common\Model\MessageWallModel;
use Common\Model\NewsModel;
use Org\Util\Date;


class MessageController extends AdminbaseController
{
    protected $wall_model;
    protected $message_model;

    function _initialize()
    {
        parent::_initialize();
        $this->wall_model    = D("Common/MessageWall");
        $this->message_model = D("Common/Messages");
    }

    //留言墙 列表
    function Index()
    {
        $currentPage = I('param.p', 0);
        if ($currentPage <= 0) {
            $currentPage = 1;
        }

        $pageStart = ($currentPage - 1) * 10;

        $list = $this->wall_model->getList($pageStart,10);
        //分页
        $page = $this->page($list['count'], 10);
        $this->assign("page", $page->show('Admin'));


        $this->assign("list",$list['data']);
        $this->display();
    }

    //添加留言墙
    public function addwall(){
        if($_POST){
            //添加 留言墙
            $data = array();
            $data['title']          = I("wall_name") ? I("wall_name") : '';
            $data['status']         = I("status") ? I("status") : '0';
            $data['set_view_count'] = I("set_view_count") ? I("set_view_count") : '0';
            $data['set_mes_count']  = I("set_mes_count") ? I("set_mes_count") : '0';
            $data['radio']          = I("set_radio") ? I("set_radio") : '';  //广播
            $data['create_time']    = date("Y-m-d H:i:s");

            $res_id = $this->wall_model->add($data);

            $data1 = array();
            if($_SERVER['SERVER_NAME'] == "xhwadmin.com"){
                $host_name = "xhw.com";
            }else if($_SERVER['SERVER_NAME'] == "kfadmin.xinhuiwen.com"){
                $host_name = "kfapi.xinhuiwen.com";
            }else if($_SERVER['SERVER_NAME'] == "fzadmin.xinhuiwen.com"){
                $host_name = "kfapi.xinhuiwen.com";
            }else if($_SERVER['SERVER_NAME'] == "admin.xinhuiwen.com"){
                $host_name = "live.lawnewsw.com";
            }
            $data1['url']  = "http://".$host_name."/Home/Messagewall?id=".$res_id;
            $this->wall_model->edit($res_id,$data1);

            $this->success("添加成功",U("Admin/Message/Index"));
        }else{
            //展示页面
            $this->display();
        }
    }
    //编辑 留言墙
    public function editwall(){
        $id = I("wall_id") ? I("wall_id") : '';
        if(I("wall_edit") == 1){
            //编辑
            $data['title']          = I("wall_name") ? I("wall_name") : '';
            $data['status']         = I("status") ? I("status") : '0';
            $data['set_view_count'] = I("set_view_count") ? I("set_view_count") : '0';
            $data['set_mes_count']  = I("set_mes_count") ? I("set_mes_count") : '0';
            $data['radio']          = I("set_radio") ? I("set_radio") : '';  //广播

            $this->wall_model->edit($id,$data);
            
            $this->success("修改成功",U("Admin/Message/Index"));
        }else{
            $info = $this->wall_model->getOne($id);
            $this->assign("info",$info);
            $this->display("editwall");
        }
        
    }
    
    //设置数量
    public function editmember(){
        $id = I("id");
        $set_view_count = I("set_view_count");
        $set_mes_count  = I("set_mes_count");

        if (empty($id)) {
            $this->error("参数错误！");
        }
        if(I("edit") == 1){
            //修改
            $data = array();
            $data['set_view_count'] = $set_view_count;
            $data['set_mes_count']  = $set_mes_count;
           
            $res = $this->wall_model->edit($id,$data);
            echo $res;
        }else{
            $this->assign("set_view_count",$set_view_count);
            $this->assign("set_mes_count",$set_mes_count);
            $this->assign("id", $id);
            $this->display('editmember');
        }
    }

    //更新状态
    public function editstatus(){
        $id = I("id");
        $status = I("status");

        if (empty($id)) {
            $this->error("参数错误！");
        }
        if(I("edit") == 1){
            //修改
            $data = array();
            $data['status'] = $status;

            $res = $this->wall_model->edit($id,$data);
            echo $res;
        }else{
            $this->assign("status",$status);
            $this->assign("id", $id);
            $this->display('editstatus');
        }
    }



    //留言管理
    public function manage(){
        $currentPage = I('param.p', 0);
        if ($currentPage <= 0) {
            $currentPage = 1;
        }

        $pageStart = ($currentPage - 1) * 10;

        $wall_id = I("wall_id");
        $list = $this->message_model->getList($wall_id,$pageStart,10);

        //分页
        $page = $this->page($list['count'], 10);
        $this->assign("page", $page->show('Admin'));

        //类型 0 测试用户，1现场观众，2线上用户，3奖池用户，4其它
        $types = array(
            0=>array("id"=>"0","name"=>"测试用户"),
            1=>array("id"=>"1","name"=>"现场观众"),
            2=>array("id"=>"2","name"=>"线上用户"),
            3=>array("id"=>"3","name"=>"奖池用户"),
            4=>array("id"=>"4","name"=>"其它"),
            );

        //查看墙的信息 type_ids
        $info = $this->wall_model->getOne($wall_id);

        $type_arr = explode(',',$info['type_ids'] );

        foreach ($types as $key=>$val){
            if(in_array($val['id'],$type_arr)){
                $types[$key]['is_show'] = 1;
            }
        }

        $this->assign("fresh_time",$info['fresh_time']);
        $this->assign("types",$types);
        $this->assign("wall_id",$wall_id);
        $this->assign("list",$list['data']);
        $this->display();
    }
    
    //添加 留言
    public function addmessage(){
        $wall_id = I("wall_id");

        if(I("message_name")){
            $data['user_name'] = I("message_name") ? I("message_name") : '';
            $data['source_id'] = I("source") ? I("source") : '';
            if(I("source") == 0){
                $data['source'] = "微信用户";
            }else if(I("source") == 1){
                $data['source'] = "APP用户";
            }else{
                $data['source'] = "竞猜用户";
            }

            $data['type_id'] = I("type") ? I("type") : '';
            if(I("type") == 0){
                $data['type'] = "测试用户";
            }else if(I("type") == 1){
                $data['type'] = "现场观众";
            }else if(I("type") == 2){
                $data['type'] = "线上用户";
            }else if(I("type") == 3){
                $data['type'] = "奖池用户";
            }else if(I("type") == 4){
                $data['type'] = "其它";
            }

            if($_FILES['imageFile']['name']){
                $newsModel = new NewsModel();  //上传封面图
                $imageUrl  = $newsModel->uploadImage();
                $data['photo'] = $imageUrl;
            }
            $data['content'] = I("content") ? I("content") : '';
            $data['wall_id'] = $wall_id;
            $data['create_time'] = date("Y-m-d H:i:s");


            $this->message_model->add($data);
            //添加 留言墙 内留言数量 和随机加设置留言数
            $wall_info = $this->wall_model->getOne($wall_id);
            $data1['message_count'] = $wall_info['message_count'] + 1;
            $this->wall_model->edit($wall_id,$data1);

            $this->success("添加成功",U("Admin/Message/manage?wall_id=$wall_id"));

        }else{
            $this->assign("wall_id",$wall_id);
            $this->display();
        }

    }
    
    //设置 留言墙 类型 
    public function settypes(){
        $wall_id  = I("wall_id");
        $data['type_ids']   = I("type_checbox") ? implode(',',I("type_checbox")) : '0,1,2,3,4';
        $data['fresh_time'] = I("set_time");

        $this->wall_model->edit($wall_id,$data);
        
        $this->success("设置成功",U("Admin/Message/manage?wall_id=$wall_id"));
    }
    
    //编辑 留言
    public function editmessage(){
        $wall_id = I("wall_id");
        $id      = I("id");
        if(I("edit_id") == 1){
            //编辑内容
            $data['user_name'] = I("message_name") ? I("message_name") : '';
            $data['source_id'] = I("source") ? I("source") : '';
            if(I("source") == 0){
                $data['source'] = "微信用户";
            }else if(I("source") == 1){
                $data['source'] = "APP用户";
            }else{
                $data['source'] = "竞猜用户";
            }

            $data['type_id'] = I("type") ? I("type") : '';
            if(I("type") == 0){
                $data['type'] = "测试用户";
            }else if(I("type") == 1){
                $data['type'] = "现场观众";
            }else if(I("type") == 2){
                $data['type'] = "线上用户";
            }else if(I("type") == 3){
                $data['type'] = "奖池用户";
            }else if(I("type") == 4){
                $data['type'] = "其它";
            }

            if($_FILES['imageFile']['name']){
                $newsModel = new NewsModel();  //上传封面图
                $imageUrl  = $newsModel->uploadImage();
                $data['photo'] = $imageUrl;
            }
            $data['content'] = I("content") ? I("content") : '';

            $this->message_model->editmes($id,$data);
            $this->success("编辑成功",U("Admin/Message/manage?wall_id=$wall_id"));
        }else{
            //显示内容
            $info = $this->message_model->getOne($id);
            $this->assign("info",$info);
            $this->assign("wall_id",$wall_id);
            $this->display();
        }
    }
    
    //修改 留言 类型
    public function editmestype(){
        $id = I("id");
        $type_id = I("type_id");

        if (empty($id)) {
            $this->error("参数错误！");
        }
        if(I("edit") == 1){
            //修改
            //类型 0 测试用户，1现场观众，2线上用户，3奖池用户，4其它
            $types = array(
                0=>array("id"=>"0","name"=>"测试用户"),
                1=>array("id"=>"1","name"=>"现场观众"),
                2=>array("id"=>"2","name"=>"线上用户"),
                3=>array("id"=>"3","name"=>"奖池用户"),
                4=>array("id"=>"4","name"=>"其它"),
            );
            $data = array();
            $data['type_id'] = $type_id;
            foreach($types as $key=>$val){
                if($val['id'] == $type_id){
                    $data['type'] = $val['name'];
                    break;
                }
            }
            $res = $this->message_model->editmes($id,$data);
            echo $res;
        }else{
            $this->assign("id", $id);
            $this->assign("type_id", $type_id);
            $this->display('editmestype');
        }
    }

    //删除 留言
    public function del(){
        $wall_id = I('wall_id');
        $id = I('param.id', "");
        if (empty($id)) {
            $this->error("参数错误！");
        }
        $result = $this->message_model->mesdel($id);
        if ($result) {
            //减少 留言墙 内留言数量
            $wall_info = $this->wall_model->getOne($wall_id);
            $data1['message_count'] = $wall_info['message_count'] - 1;
            $this->wall_model->edit($wall_id,$data1);

            $this->success("删除成功！",U("Admin/Message/manage?wall_id=$wall_id"));
        } else {
            return $this->error($result['Message']);
        }
    }

}